<?php


namespace app\api\task;

use app\api\model\Message_prompt;
use app\api\model\User;
use app\common\lib\Export;
use think\facade\Log;
use app\common\facade\WebSocketToken;
use think\Container;

class Prompt extends Base
{
    protected $cmd_data = [
        "100"=>"save_prompt"
    ];

    public function __construct($cmd,$data)
    {
        parent::__construct($cmd,$data);
    }
    public function run(){
        $run_method = $this->run_method;
        $this->$run_method();
    }
    /**
     * 保存消息提示
     *   type 0，关注；1，动态点赞；2，动态评论；3，评论点赞；4，评论评论；5，加入团队；6.退出团队
     * */
    public function save_prompt(){
        // 自我操作不提示
        if($this->run_data['user_id'] == $this->run_data['operate_user_id']) return true;

        //日志写入
        Log::info("开始保存消息提示：".json_encode($this->run_data));

        //用户是否在线
        $fd = WebSocketToken::get_fd($this->run_data['user_id']);
        $exist = WebSocketToken::exist($fd);
        $is_read = $exist?1:0;

        // 类似的操作 ：0，关注；1，动态点赞；3，评论点赞
        if(in_array($this->run_data['type'],Message_prompt::$auto_increment_type)){
            // 查找类似的操作提示
            $where = [
                "user_id"=>$this->run_data['user_id'],
                "object_id"=>$this->run_data['object_id'],
                "type"=>$this->run_data['type'],
                "is_read"=>$is_read,
            ];
            $prompt = Message_prompt::where($where)->find();

            if($prompt){
                // 存在类似操作
                if($this->run_data['increment']){
                    // 增量操作
                    $prompt->number = intval($prompt->number) + 1;
                }else{
                    // 取消操作
                    $prompt->number = intval($prompt->number) - 1;
                }
                // 如果取消操作后，类似的操作为0 则删除此操作
                !$prompt->number && $res = $prompt->delete();

            }else{
                if($this->run_data['increment']){
                    $res = Message_prompt::create([
                        "user_id"=>$this->run_data['user_id'],
                        "object_id"=>$this->run_data['object_id'],
                        "type"=>$this->run_data['type'],
                        "operate_user_id"=>$this->run_data['operate_user_id'],
                        "operate_id"=>$this->run_data['operate_id'],
                        "is_read"=>$is_read,
                    ]);
                }
            }
        }else{
            // 其他操作直接新增
            $res = Message_prompt::create([
                "user_id"=>$this->run_data['user_id'],
                "object_id"=>$this->run_data['object_id'],
                "type"=>$this->run_data['type'],
                "operate_user_id"=>$this->run_data['operate_user_id'],
                "operate_id"=>$this->run_data['operate_id'],
                "is_read"=>$is_read,
            ]);
        }

        //即时提醒
        if($exist){
            $operate_user = User::get($this->run_data['operate_user_id']);
            $info = [
                "data"=>[
                    "user"=>[
                        "id"=>$operate_user->id,
                        "name"=>$operate_user->name,
                        "pic"=>$operate_user->pic_url,
                    ],
                    "type"=>$this->run_data['type'],
                    "is_cancel"=>$this->run_data['increment']?0:1
                ]
            ];
            Export::socket_exp_ok($fd,Export::$socket_prompt_cmd,$info);
        }

        //日志写入
        Log::info("保存消息提示成功：".json_encode($this->run_data));


        return $res;
    }
    /** ---------------------   内部实现方法--------------------- */

}